{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入需要的模块\n",
    "import requests\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 输入密钥\n",
    "KEY = '0e06f12a31564df180fd4183d0426ede'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 目标URL\n",
    "BASE_URL = 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请求头\n",
    "HEADERS = {\n",
    "    'Content-Type': 'application/json',\n",
    "    'Ocp-Apim-Subscription-Key': '{}'.format(KEY), \n",
    "}\n",
    "\n",
    "# 人脸相片地址\n",
    "img_url = 'https://www.apple.com.cn/newsroom/images/live-action/keynote-september-2020/apple_apple-event-keynote_tim_09152020_big.jpg.large_2x.jpg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {\n",
    "    'url': '{}'.format(img_url),\n",
    "}\n",
    "\n",
    "# 选择需要的人脸识别功能（根据API文档）\n",
    "payload = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'flase',\n",
    "    'returnFaceAttributes': '{}'.format('age,gender,glasses,emotion'), \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 发送请求\n",
    "r = requests.post(BASE_URL,data=json.dumps(data),params = payload,headers=HEADERS)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "200"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.status_code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'[{\"faceId\":\"6d9d597e-fac5-4f29-87a0-68060978df50\",\"faceRectangle\":{\"top\":121,\"left\":571,\"width\":146,\"height\":146},\"faceAttributes\":{\"gender\":\"male\",\"age\":60.0,\"glasses\":\"ReadingGlasses\",\"emotion\":{\"anger\":0.003,\"contempt\":0.002,\"disgust\":0.002,\"fear\":0.0,\"happiness\":0.005,\"neutral\":0.986,\"sadness\":0.001,\"surprise\":0.0}}}]'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'faceId': '6d9d597e-fac5-4f29-87a0-68060978df50',\n",
       "  'faceRectangle': {'top': 121, 'left': 571, 'width': 146, 'height': 146},\n",
       "  'faceAttributes': {'gender': 'male',\n",
       "   'age': 60.0,\n",
       "   'glasses': 'ReadingGlasses',\n",
       "   'emotion': {'anger': 0.003,\n",
       "    'contempt': 0.002,\n",
       "    'disgust': 0.002,\n",
       "    'fear': 0.0,\n",
       "    'happiness': 0.005,\n",
       "    'neutral': 0.986,\n",
       "    'sadness': 0.001,\n",
       "    'surprise': 0.0}}}]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# JSON转义\n",
    "results = r.json()\n",
    "results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 用Pandas简化数据\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.json_normalize(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>faceId</th>\n",
       "      <th>faceRectangle.top</th>\n",
       "      <th>faceRectangle.left</th>\n",
       "      <th>faceRectangle.width</th>\n",
       "      <th>faceRectangle.height</th>\n",
       "      <th>faceAttributes.gender</th>\n",
       "      <th>faceAttributes.age</th>\n",
       "      <th>faceAttributes.glasses</th>\n",
       "      <th>faceAttributes.emotion.anger</th>\n",
       "      <th>faceAttributes.emotion.contempt</th>\n",
       "      <th>faceAttributes.emotion.disgust</th>\n",
       "      <th>faceAttributes.emotion.fear</th>\n",
       "      <th>faceAttributes.emotion.happiness</th>\n",
       "      <th>faceAttributes.emotion.neutral</th>\n",
       "      <th>faceAttributes.emotion.sadness</th>\n",
       "      <th>faceAttributes.emotion.surprise</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>6d9d597e-fac5-4f29-87a0-68060978df50</td>\n",
       "      <td>121</td>\n",
       "      <td>571</td>\n",
       "      <td>146</td>\n",
       "      <td>146</td>\n",
       "      <td>male</td>\n",
       "      <td>60.0</td>\n",
       "      <td>ReadingGlasses</td>\n",
       "      <td>0.003</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.002</td>\n",
       "      <td>0.0</td>\n",
       "      <td>0.005</td>\n",
       "      <td>0.986</td>\n",
       "      <td>0.001</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                 faceId  faceRectangle.top  \\\n",
       "0  6d9d597e-fac5-4f29-87a0-68060978df50                121   \n",
       "\n",
       "   faceRectangle.left  faceRectangle.width  faceRectangle.height  \\\n",
       "0                 571                  146                   146   \n",
       "\n",
       "  faceAttributes.gender  faceAttributes.age faceAttributes.glasses  \\\n",
       "0                  male                60.0         ReadingGlasses   \n",
       "\n",
       "   faceAttributes.emotion.anger  faceAttributes.emotion.contempt  \\\n",
       "0                         0.003                            0.002   \n",
       "\n",
       "   faceAttributes.emotion.disgust  faceAttributes.emotion.fear  \\\n",
       "0                           0.002                          0.0   \n",
       "\n",
       "   faceAttributes.emotion.happiness  faceAttributes.emotion.neutral  \\\n",
       "0                             0.005                           0.986   \n",
       "\n",
       "   faceAttributes.emotion.sadness  faceAttributes.emotion.surprise  \n",
       "0                           0.001                              0.0  "
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
